-- 不建议使用
-- 初始化的内容都在内存中，太大了，用 UiMain.lua 的返回值比较好

UiVa = {
  ui = {},
  store = {},
}

function UiVaPrintLog(msg)
  -- local dev = developEnv
  local dev = 2
  if dev == nil then
    RxLog('UiVaPrintLo -dev='..tostring(dev))
    dev = 1
  end
  if dev == 0 or dev == 10 then
    RxLog(msg)
  end
end

-- 采用 table 配置的UI
function UiVaUpdateTableValue(cs, plugins)
  if not cs then return end

  local ui = {}
  local uc = UiCore.idkv

  local comboxLinkAge = {}
  local comboxLinkAgeSub = {}

  for k,v in pairs(cs) do
    UiVaPrintLog('UiVa k='..tostring(k)..' --v='..tostring(v))
    local newValue = v
    local config = uc[k]
    if config and v and v ~= '' then
      local isTab = config.isTab
      local isCombSubData = config.isCombSubData
      if isTab then
        UiVaPrintLog('-----isTab----')
        local list = config.list
        local isMutil = config.isMutil
        if isMutil then
          local muArr = UtilStringSplit(v, '@')
          for k2,v2 in pairs(muArr) do
            UiVaPrintLog('k='..tostring(k)..' -k2='..tostring(k2)..' --v2='..tostring(v2)..' -list='..tostring(list))
            v2 = v2 or '0'
            local index = tonumber(v2) + 1
            UiVaPrintLog('index='..tostring(index))
            newValue = list[index]
            local newkey = k .. index
            ui[newkey] = newValue
          end
        else
          newValue = list[v + 1]
          ui[k] = newValue
          local data = config.data
          if data ~= '' then
            local newKey = k ..'_num'
            local vNum = tonumber(v) + 1
            ui[newKey] = vNum
            comboxLinkAge[k] = vNum
          end
        end
      elseif isCombSubData then
        UiVaPrintLog('-----isCombSubData----')
        local list = config.list
        local muArr = UtilStringSplit(list, '#')
        local dataList = {}
        local index = 1
        for kk2,kv2 in pairs(muArr) do
          UiVaPrintLog('k='..tostring(k)..' -kk2='..tostring(kk2)..' --kv2='..tostring(kv2)..' -list='..tostring(list))
          local dataSub = {}
          local k2MuArr = UtilStringSplit(kv2, ',')
          for k2k2,k2v2 in pairs(k2MuArr) do
            UiVaPrintLog('-k2k2='..tostring(k2k2)..' --k2v2='..tostring(k2v2))
            dataSub[k2k2] = k2v2
          end
          dataList[index] = dataSub
          index = index + 1
        end
        local numValue = tonumber(v) + 1
        newKey = k .. '_num'
        ui[newKey] = numValue
        local rootId = config.rootId
        UiVaPrintLog('------------ isCombSubData rootId='..tostring(rootId))
        if rootId and rootId ~= '' then
          comboxLinkAgeSub[rootId] = {
            list = dataList,
            key = k,
            select = numValue
          }
        end
      else
        UiVaPrintLog('---not---isTab and isCombSubData----')
        ui[k] = newValue
      end
    end
  end

  -- 多级联动
  for k,v in pairs(comboxLinkAge) do
    UiVaPrintLog('UiVa.comboxLinkAge k='..tostring(k)..' --v='..tostring(v))
    local subList = comboxLinkAgeSub[k] or nil
    if subList then
      local list = subList['list'] or nil
      local subSelect = subList['select'] or 0
      local len = RxTable:len(list)
      local key = subList.key or 'unknow'
      local vNum = tonumber(v)
      UiVaPrintLog('UiVa.comboxLinkAge subSelect='..tostring(subSelect)..'--key='..tostring(key)..'--len='..tostring(len)..'--vNum='..tostring(vNum))
      if list and subSelect ~= nil and vNum <= len then
        local subList = list[vNum] or nil
        local subLen = RxTable:len(subList)
        if subList and subLen > 0 then
          local newValue = subList[subSelect]
          UiVaPrintLog('UiVa.comboxLinkAge newValue='..tostring(newValue))
          if newValue then
            ui[key] = newValue
          end
        end
      else
        UiVaPrintLog('UiVa.comboxLinkAge ageSub{}-len='..tostring(len))
      end
    end
  end

  if plugins and type(plugins) == 'function' then
    ui = plugins(ui)
  end
  UiVaAllConfigMerge(ui)
end

function UiVaUINew()
  local ui = {}
  -- 全天自动模式
  ui.threadAuto = rxthreadAuto

  -- 模式
  ui.rdo = rxrdo

  ---------------------------------------
  -- 敦煌
  -- 敦煌跑图记录保留天数
  ui.dh_saveday = rxdh_saveday

  -- 敦煌 - 荭包 - 是否自动领取
  ui.dhRedbag = rxdh_saveday

  -- 敦煌悬浮框-显示内容 扩展选择
  ui.dhFloat1 = rxdhFloat1
  ui.dhFloat2 = rxdhFloat2
  ui.dhFloat3 = rxdhFloat3
  ui.dhFloat4 = rxdhFloat4

  -- 主公-当前官衔
  ui.dh_guanxian = rxdh_guanxian
  -- 主公-当前等级
  ui.dh_dengji = rxdh_guanxian

  -- 神神叨叨的胖子
  ui.dh_ssdddpz = rxdh_ssdddpz
  -- 奇异的水池
  ui.dh_qydsc = rxdh_qydsc
  -- 枯萎的植物
  ui.dh_kwdzw = rxdh_kwdzw
  -- 华丽的宝箱
  ui.dh_hldbx = rxdh_hldbx

  -- 神秘行商 - 不买
  ui.dh_smxs1 = rxdh_smxs1
  -- 神秘行商 - 玄天八卦
  ui.dh_smxs2 = rxdh_smxs2

  -- 探险者 - 挑战
  ui.dh_txz = rxdh_txz
  -- 可疑的男子
  ui.dh_kydnz = rxdh_kydnz
  -- 粗心的探宝者
  ui.dh_cxdtbz = rxdh_cxdtbz
  --------------------------------------
  -- 日常
  -- 福利-五谷丰登
  ui.rc_wgfd1 = rxrc_wgfd1
  ui.rc_wgfd2 = rxrc_wgfd2
  ui.rc_wgfd3 = rxrc_wgfd3
  ui.rc_wgfd4 = rxrc_wgfd4
  ui.rc_wgfd5 = rxrc_wgfd5
  ui.rc_wgfd6 = rxrc_wgfd6
  ui.rc_wgfd7 = rxrc_wgfd7

  -- 礼品码 - 如果是 [游卡官方游戏] 可`开启`
  ui.rc_user_giftcode = 'off'

  -- 点瓒 - 仅给 `战力榜` 前3名
  ui.rc_support = 'on'

  -- 召唤-身外化身-消耗所有-玄天八卦
  ui.rc_summon = rxrc_summon
  -- 军团-自修
  ui.rc_legion_zx = rxrc_legion_zx
  -- 军团-修炼-挑战选择
  ui.rc_legion_trial = '0-最底层'

  -- 神树 - 桃园
  ui.rc_shenshu_ty_fetchWater = 'off'
  -- 神树 - 桃园 - 钓鱼 (使用全部)
  ui.rc_shenshu_ty_goFishing = 'off'
  -- 神树 - 桃园 - 钓鱼-使用[渔网] (使用全部)
  ui.rc_shenshu_ty_use_fishingNet = 'off'

  -- 神武-领取、锻造
  ui.rc_shenWu_auto = 'on'

  -- 挑战 - 主线副本
  ui.rc_challenge_zhuxian = 'on'
  -- 挑战 - 名将副本
  ui.rc_challenge_mingjiang = 'on'
  -- 挑战 - 精英副本
  ui.rc_challenge_jingying = 'on'

  -- 冒险 - 自动挑战(3个武将 +1个宝箱)
  ui.rc_adventure_start = 'on'
  --------------------------------------
  -- 特殊活动
  -- 个人竞技(跨服)
  ui.av_geRenjj = 'on'

  -- 天下无双
  ui.av_tianXiaWS = 'on'

  -- 跨服军团对战
  ui.av_kuafujtdz = 'on'

  -- 饕餮盛宴
  ui.av_taoTieSY = 'on'

  -- 奕定乾坤
  ui.av_yidingQK = 'on'
  ui.av_taoTieSY_enroll = 'on'

  -- 汉将 - 世之齐佐
  ui.av_hangjiang_shiZhiQZ = 'on'

  -- 琳琅幻境
  ui.av_linLangHJ = 'on'

  -- 金色变身卡 - 镜花水月
  ui.av_jingHuaSY = 'on'

  -- 畅爽盛夏
  ui.av_changshuangSX = 'on'

  -- 合区庆典
  ui.av_heFuQD = 'on'

  --------------------------------------

  UiVaAllConfigMerge(ui)
end

function UiVaAllConfigMerge(ui)
  -- 多号-处理入口问题
  local threadAuto = ui.threadAuto or 'off'
  local youka_auto = ui.youka_auto or 'off'
  if youka_auto == 'on' then
    ui.threadAuto = 'off'
    ui.rdo = '每日任务'
  end

  local rdo = ui.rdo or ''
  UiVaPrintLog('UiVaAllConfigMerge ui.rdo='..tostring(rdo))

  local isSigleDh = false
  if rdo == "接单模式" or rdo == '接单(1次)' or rdo == '标准模式' or rdo == '无尽模式' or rdo == '无尽(1次)' then
    isSigleDh = true
    -- if developEnv == 1 or developEnv == 0 then
    --   -- 多线程的全局变量，一定要在 声明[后]，[再]给变量赋值
    --   createGobalTable("UiVa")
    -- end
  end

  UiVa.ui = ui
  UiVa.sysInit = sysInit

  local isTest = false
  local youka_test_auto = ui.youka_test_auto or 'off'
  if youka_test_auto == 'on' then
    isTest = true
  end

  UiAccount:init(ui, isTest)

  local UiAccountOn = UiAccount:isUse()
  -- 蔬菜截图
  local UiAccountShuCaiOn = true
  local UiAccountMiniOn = false

  if not UiAccountOn then
    UiVaPrintLog('--UiVa-Merge------- UiAccountOn = false')
    UiAccountShuCaiOn = false
  else
    UiVaPrintLog('--UiVa-Merge------- UiAccountOn = true')
    local U_A_shucai = ui.yk_knapsack_shuCai or 'off'
    if U_A_shucai == 'off' then
      UiVaPrintLog('--UiVa-Merge-------- U_A_shucai = off')
      UiAccountShuCaiOn = false
    end

    local youka_mini = ui.youka_mini or 'off'
    if youka_mini == 'on' then
      UiAccountMiniOn = true
    end
  end

  UiVaPrintLog('--UiVa-Merge--------UiAccountMiniOn='..tostring(UiAccountMiniOn))

  UiVa.ui.isTest = isTest
  UiVa.UiAccount = {
    mini = UiAccountMiniOn,
    users = UiAccount.users,
    on = UiAccountOn,
    count = UiAccount.count,
    shucaiOn = UiAccountShuCaiOn,
    logout = false,
    select = 0,
    logoutPreviousIndex = 0,
  }

  UiVa.ui.dhOnce = 0
  UiVa.ui.dhFull = 0

  UiVa.game = {
    currentBid = nil, -- 游戏id
    currentUid = nil, -- 用户id,
  }

  UiVa.user = {
    id = nil,
    level = nil,
  }

  UiVa.page = {
    avAnswer = nil,
  }
end